package com.erp.util.db;

import java.sql.*;

/**
 * @author lpy
 * 数据库连接工具类
 */
public class DataBaseDao {
    private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DBUSER = "root";
    private static final String DBPASS = "mldn";
    private static final String DBURL = "jdbc:mysql://172.3.18.62:3306/erp?characterEncoding=utf-8";

    protected Connection conn;
    protected PreparedStatement pstmt;
    protected ResultSet rs;

    /**
     * 加载数据库连接
     */
    protected void buildConnection(){
        try {
            //加载驱动
            Class.forName(DBDRIVER);
            //获取连接
            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected DataBaseDao() {
        //buildConnection();
    }

    /**
     * 关闭资源
     */
    protected void close() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}

